/**
 * Created by KY on 2017/7/20.
 */
define(['underscore'],function(underscore){
    function BaseCtrl(){
        this.$container = $('.container')
        this.initialize.apply(this, arguments);
    }
    BaseCtrl.prototype = {
        initialize:function(){
            //最多缓存2个页面
            // console.log(`是否使用了代理? ${window.Proxy && window.Reflect && true}`)
            if(window.Proxy && window.Reflect){
                let pageStack = [];
                let handler = {
                    set(target, key, value, proxy){
                        if(target.length>2){
                            let page = target.splice(0,1)
                            page[0].view.$el.remove()
                            if(key==='length' && value > 2){
                                return 2;
                            }
                        }
                        return Reflect.set(target, key, value, proxy);
                    }
                }
                this._pageStack = new Proxy(pageStack,handler)
            }else{
                this._pageStack = []
            }
        },
        _destroy:function(){
        },
        _show:function(page){
            var self = this
            page.view.trigger('before-shown');
            var view = page.view.render()
            var el = view.el;
            var className = page.url.split('#')[1]
            var $el = view.$el.addClass(className+'-panel').addClass('slideIn')
            $el.one('animationend webkitAnimationEnd', function(){
                $el.removeClass('slideIn').addClass('active');
                view.trigger('shown')
            })
            this.$container.append($el)
            if(!window.Proxy || !window.Reflect) {
                if(this._pageStack.length>=2){
                    this._pageStack.splice(0,1)
                }
            }
            this._pageStack.push({
                url:page.url,
                view:view
            })
        },
        _back:function(page){
            var stack = this._pageStack.pop();
            if (!stack) {
                return
            }
            var url = location.hash.indexOf('#') === 0 ? location.hash : '#';
            var found = this._findInStack('index'+url);
            if (!found){
                var el = page.view.el;
                var $el = page.view.$el.addClass('active');
                $el.insertBefore(stack.view.$el);
                this._pageStack.push(page)
            }
            if(page.url === stack.url){
                return
            }else{
                stack.view.$el.addClass('slideOut').one('animationend webkitAnimationEnd', function () {
                    // console.log('我进来了么？？？')
                    stack.view.$el.removeClass('slideOut').removeClass('active');
                    stack.view.trigger('hidden')
                    stack.view.$el.remove()
                    page.view.trigger('shown')
                })
            }
        },
        _findInStack:function(url){
            var found = null;
            for(var i = 0, len = this._pageStack.length; i < len; i++){
                var stack = this._pageStack[i];
                if (stack.url === url) {
                    found = stack;
                    break;
                }
            }
            return found;
        }
    }

    BaseCtrl.extend = function(protoProps) {
        var parent = this;
        var child;
        if(protoProps && protoProps.hasOwnProperty('constructor')){
            child = protoProps.constructor;
        }else{
            child = function(){
                return parent.apply(this,arguments);
            }
        }
        child.prototype = _.create(parent.prototype, protoProps);
        child.prototype.constructor = child;
        child.__super__ = parent.prototype;
        return child;
    };
    return BaseCtrl;
})